/*
 * Created on 02-dic-2004
 *
 */
package guru.mercurio.data;

import guru.mercurio.common.AbsoluteApplicationAddress;
import guru.mercurio.common.RelativeApplicationAddress;

//import org.apache.log4j.Logger;
//Comentado por santiago el 04/04/2006 para evaluar el uso de la libreria DBCP
//import com.bitmechanic.sql.ConnectionPoolManager;


/**
 * @author cobach
 *
 */
public class ChannelDataManager {
    public DataManager dm;
    //private static Logger logger = Logger.getLogger(ChannelDataManager.class);

    public ChannelDataManager(String driver, String dbmsURL, String username, String password) throws Exception{
        dm=new DataManager(driver,dbmsURL,username,password);
    }
    public ChannelDataManager(DataManager dm) throws Exception{
        this.dm=dm;
    }

    //*******************************************************************************************************

    public void createOperator(String shortName, String name, int countryCode, int areaCode) throws Exception{
        String[] columns={"shortName","name","country","area","status","operator_id"};
        Object[] values={shortName,name,new Integer(countryCode),new Integer(areaCode),new Integer(0),dm.getNextInSequence("operator","operator_id")};

        dm.insertRow("operator", columns, values );  
    }
    
    public int setOperatorStatus(String shortName, int status) throws Exception{
        String[] columns={"status"};
        Object[] values={new Integer(status)};
        return(dm.setValues("operator",columns ,values,"shortName",shortName));        
    }
    
    public int getOperatorStatus(String shortName) throws Exception{
        return(((Integer)(dm.getValue("operator","status","shortName",shortName))).intValue());
    }

    
    //*******************************************************************************************************

    /**
     * @deprecated Debe utilizar public void createPort(String id, String operatorShortName, String mode)
     */
    public void createPort(String id, String operatorShortName) throws Exception{
        createPort(id, operatorShortName, "yes");
    }
    
    public void createPort(String id, String operatorShortName, String transmitter) throws Exception{
        Integer operator_id=(Integer)dm.getValue("operator","operator_id","shortName",operatorShortName);

        String[] columns={"id","status","port_id","operator_id","transmitter"};
        Object[] values={id,new Integer(0),dm.getNextInSequence("port","port_id"),operator_id,transmitter};

        dm.insertRow("port", columns, values );
    }

    public int setPortId(String oldPortId, String newPortId) throws Exception{
        String[] columns={"id"};
        Object[] values={newPortId};
        return(dm.setValues("port",columns ,values,"number",oldPortId));
    }    

    public int setPortStatus(String portId, int status) throws Exception{
        String[] columns={"status"};
        Object[] values={new Integer(status)};
        return(dm.setValues("port",columns ,values,"id",portId));        
    }

    public int getPortStatus(String portId) throws Exception{
        return(((Integer)(dm.getValue("port","status","id",portId))).intValue());
    }

    //*************************************************************************
    
    public void createChannel(String alias, String channelId,String subchannel,String passport) throws Exception{
        Long channel_id=dm.getNextInSequence("channel","channel_id");
        Integer passport_id=(Integer)dm.getValue("passport","passport_id","id",passport);
        
        String[] columns={"id","channel_id","subchannel","alias","passport_id","default_passport_id"};
        Object[] values={channelId,channel_id,subchannel,alias,passport_id,passport_id};

        dm.insertRow("channel", columns, values );
        
        
    }


    public int setChannelNumber(String oldPortId, String newPortId) throws Exception{
        String[] columns={"id"};
        Object[] values={newPortId};
        return(dm.setValues("channel",columns ,values,"id",oldPortId));
    }    


    public void addPort2Channel(String channelId, String subchannel, String portId, String operatorShortName) throws Exception{
        Integer operator_id=(Integer)dm.getValue("operator","operator_id","shortName",operatorShortName);
        
        
        String[] refNames={"id","subchannel"};
        Object[] refValues={channelId,subchannel};
        
        Integer channel_id=(Integer)dm.getValue("channel","channel_id",refNames,refValues);

        String[] columns2={"id","operator_id"};
        Object[] values2={portId,operator_id};
        Integer port_id=(Integer)dm.getValue("port","port_id",columns2,values2);

        
        String[] columns3={"port_id","channel_id"};
        Object[] values3={port_id,channel_id};
        
        dm.insertRow("channel_port", columns3, values3 );
    
    }

    public void setDefaultChannel (String passportId, String channelId, String subchannel) throws Exception{
        dm.setDefaultChannel2(passportId,channelId,subchannel);
    }
    
    //public void removePortFromChannel(int channelNumber,String portNumber, String operatorShortName){
    //}

    
    
    //*************************************************************************


    public int assignChannel(String channel, String passport) throws Exception{
        Integer passport_id=(Integer)dm.getValue("passport","passport_id","id",passport);

        String[] columns={"passport_id"};
        Object[] values={passport_id};
        return(dm.setValues("channel",columns ,values,"id",channel));
    }

    
  
    public int deassignChannel(String channel) throws Exception{
        String[] columns={"passport_id"};
        Object[] values={null};
        return(dm.setValues("channel",columns ,values,"id",channel));
    }


    public int assignDefaultChannel(String channel, String passport) throws Exception{
        Integer passport_id=(Integer)dm.getValue("passport","passport_id","id",passport);

        String[] columns={"default_passport_id"};
        Object[] values={passport_id};
        return(dm.setValues("channel",columns ,values,"id",channel));
        
    }

    
    //***********************************************************************

 
    /*
    public AbsoluteApplicationAddress getApplicationByPort(String portId,String operatorShortName) throws Exception{
        String[] refIDs={"passport.passport_id","channel_port.channel_id","channel_port.port_id","port.operator_id","operator.shortName","port.id"};
        Object[] refValues={"channel.passport_id","channel.channel_id","port.port_id","operator.operator_ID",operatorShortName,portId};

        String[] columns={"passport.id","passport.appProtocol","passport.appURL","passport.appKey"};
        
        Object[] out=dm.getValues("passport,channel,channel_port,port,operator",columns,refIDs,refValues);
        return(new AbsoluteApplicationAddress(out[0].toString(),out[1].toString(),out[2].toString(),out[3].toString()));
    }
*/
 
    
    public AbsoluteApplicationAddress[] getApplicationByChannel(String channelId) throws Exception{
        String[] refIDs={"passport.passport_id","channel.id","channel.subchannel"};
        Object[] refValues={"channel.passport_id",channelId,""};

        String[] columns={"passport.id","passport.appProtocol","passport.appURL","passport.appKey"};
        
        Object[][] in=dm.getValues("passport,channel",columns,refIDs,refValues);
        
        AbsoluteApplicationAddress[] out=new AbsoluteApplicationAddress[in.length];
        for (int i=0;i<in.length;i++){            
            out[i]=new AbsoluteApplicationAddress(in[i][0].toString(),in[i][1].toString(),in[i][2].toString(),in[i][3].toString());
        }
        return(out);
    }

    

    
    public AbsoluteApplicationAddress[] getApplicationBySubChannel(String channelId, String subchannel) throws Exception{
        String[] refIDs={"passport.passport_id","channel.id","channel.subchannel"};
        Object[] refValues={"channel.passport_id",channelId,subchannel};

        String[] columns={"passport.id","passport.appProtocol","passport.appURL","passport.appKey"};
        Object[][] in = null;
        in=dm.getValues("passport,channel",columns,refIDs,refValues);
        AbsoluteApplicationAddress[] out= null;
        out=new AbsoluteApplicationAddress[in.length];
        if (in!=null) {
            for (int i=0;i<in.length;i++){               
                out[i]=new AbsoluteApplicationAddress(in[i][0].toString(),in[i][1].toString(),in[i][2].toString(),in[i][3].toString());
            }            
        }
        return(out);
    }

    
    public String getDefaultChannel(String passportID) throws Exception{
        String[] refIDs={"passport.passport_id","passport.id"};
        Object[] refValues={"channel.default_passport_id",passportID};
        
        return((String)dm.getValue("passport,channel","channel.id",refIDs,refValues));
    }

    /*
    public String getDefaultPort(String passportID,String operatorShortName) throws Exception{
        Integer operator_id=(Integer)dm.getValue("operator","operator_id","shortName",operatorShortName);

        String[] refIDs={"passport.passport_id","passport.id","channel_port.channel_id","channel_port.port_id","port.operator_id"};
        Object[] refValues={"channel.default_passport_id",passportID,"channel.channel_id","port.port_id",operator_id};
        
        return((String)dm.getValue("passport,channel,channel_port,port","port.id",refIDs,refValues));
    }
*/
    public RelativeApplicationAddress getRelativeApplicationByPassport(String passportID,int countryCode, int areaCode) throws Exception{
        String[] refIDs={"passport.passport_id","passport.id","channel_port.channel_id","channel_port.port_id","port.operator_id","country","area","transmitter"};
        Object[] refValues={"channel.default_passport_id",passportID,"channel.channel_id","port.port_id","operator.operator_id",new Integer(countryCode),new Integer(areaCode),"yes"};
        
        String[] selector={"operator.shortName","port.id"};
        
        Object[][] result=dm.getValues("passport,channel,channel_port,port,operator",selector,refIDs,refValues);
        //logger.info("r1:"+result.length);
        //logger.info("r2:"+result[0].length);
        
        //logger.info("(String)result[0][0]"+(String)result[0][0]+"(String)result[0][1]" +(String)result[0][1]);
        return(new RelativeApplicationAddress((String)result[0][0],(String)result[0][1]));
    }

    public String getOperatorShortName(int countryCode, int areaCode) throws Exception{
        String[] refIDs={"country","area"};
        Object[] refValues={new Integer(countryCode),new Integer(areaCode)};
        
        return((String)dm.getValue("operator","shortName",refIDs,refValues));
    }

    
    //***********************************************************************
    
}
